package ga.wavaplayer.video {
	
	import flash.media.SoundTransform;
	import flash.media.Video;
	import flash.events.Event;
	import ga.wavaplayer.events.WEvent;
	import ga.wavaplayer.net.WNetConnection;
	import ga.wavaplayer.net.WNetStream;
	import ga.wemanager.WEManager;
	import ga.wavaplayer.ui.WControls;
	
	public class WVideo extends Video {
		
		public static const WVIDEO_MODE_HTTP:String = 'wvideo.wvideo_mode_http';
		public static const WVIDEO_MODE_RTMP:String = 'wvideo.wvideo_mode_rtmp';
		
		public static const WVIDEO_STATUS_ERROR:int = -1;
		public static const WVIDEO_STATUS_INACTIVE:int = 0;
		public static const WVIDEO_STATUS_BUFFERING:int = 1;
		public static const WVIDEO_STATUS_PLAYING:int = 2;
		public static const WVIDEO_STATUS_PAUSED:int = 3;
		public static const WVIDEO_STATUS_ENDED:int = 4;
		
		public static const PAUSED:String = 'wvideo.paused';
		public static const STARTED:String = 'wvideo.started';
		public static const RESUMED:String = 'wvideo.resumed';
		public static const STOPPED:String = 'wvideo.stopped';
		public static const SOUND_DISABLED:String = 'wvideo.sound_enabled';
		public static const SOUND_ENABLED:String = 'wvideo.sound_disabled';
		public static const ENDED:String = 'wvideo.ended';
		
		public var connection:WNetConnection;
		public var stream:WNetStream;
		
		protected var _url:String;
		protected var _soundTransform:SoundTransform;
		
		public var volume:Number;
		public var status:int;
		
		public function WVideo(options:Object):void {
			_soundTransform = new SoundTransform()
			stream.soundTransform = _soundTransform;
			this.attachNetStream(stream);
			
			this.smoothing = true;
			
			_url = options.url;
			
			WEManager.addEventListener(WControls.PAUSE_CLICKED, _pauseClickHandler);
			WEManager.addEventListener(WControls.PLAY_CLICKED, _playClickHandler);
			WEManager.addEventListener(WControls.SOUND0_CLICKED, _sound0ClickHandler);
			WEManager.addEventListener(WControls.SOUND1_CLICKED, _sound1ClickHandler);
			WEManager.addEventListener(WControls.VOLUME_UPDATED, _volumeUpdatedHandler);
			WEManager.addEventListener(WControls.SEEK_REQUEST, _seekRequestHandler);
		}
		
		public static function getVideo(options:Object):WVideo{
			
			var video:WVideo;
			
			if(options.mode == WVIDEO_MODE_HTTP){
				video = new WHTTPVideo(options);
			}else if(options.mode == WVIDEO_MODE_RTMP){
				video = new WRTMPVideo(options);
			}
			
			return video;
		}
		
		public function resize(pWidth:int, pHeight:int):void{
			this.width = pWidth;
			this.height = pHeight;
		}
		
		public function play():void {
			stream.play(_url);
			status = WVideo.WVIDEO_STATUS_PLAYING;
			WEManager.dispatchEvent(new Event(WVideo.STARTED));
		}
		
		public function pause():void {
			stream.pause();
			status = WVideo.WVIDEO_STATUS_PAUSED;
			WEManager.dispatchEvent(new Event(WVideo.PAUSED));
		}
		
		public function resume():void {
			stream.resume();
			status = WVideo.WVIDEO_STATUS_PLAYING;
			WEManager.dispatchEvent(new Event(WVideo.RESUMED));
		}
		
		public function seek(ratio:Number):void {}
		
		private function _pauseClickHandler(e:Event):void {
			pause();
		}
		
		private function _playClickHandler(e:Event):void {
			if(status == WVideo.WVIDEO_STATUS_PAUSED){
				resume();
			}
		}
		
		private function _sound0ClickHandler(e:Event):void {
			_soundTransform.volume = volume;
			stream.soundTransform = _soundTransform;
			WEManager.dispatchEvent(new Event(WVideo.SOUND_ENABLED));
		}
		
		private function _sound1ClickHandler(e:Event):void {
			_soundTransform.volume = 0;
			stream.soundTransform = _soundTransform;
			WEManager.dispatchEvent(new Event(WVideo.SOUND_DISABLED));
		}
		
		private function _volumeUpdatedHandler(e:WEvent):void {
			volume = e.volume;
			_soundTransform.volume = e.volume;
			stream.soundTransform = _soundTransform;
		}
		
		private function _seekRequestHandler(e:WEvent):void {
			seek(e.ratio);
		}
	}
}
